<?php
namespace Home\Controller;
use Think\Controller;
class UsersController extends Controller {
    public function reg(){
    
    	$this->display();
    }

    public function login(){
   
        $this->display();
    }

    public function checkLogin(){
          $code=I('post.verify');
     
        if (!$this->check_verify($code)) {
            $this->error('验证码错误');
        }
        $admin=M('users');
        $map = array();
        $map['telephone']  = I('post.telephone');
        $map['password']    = md5(I('post.password'));
       
        $res=$admin->where($map)->select();
/*     dump($map);
        dump($res);
        exit;*/
        if($map==null){
            $this->error('用户名密码错误');
        }else {
            session("flag","logok");
            session("telephone",$map['telephone']);
            session("id",$map['id']);
            redirect('../Food/foodlist');
        }
    }
     public function check_verify($code,$id=''){
        $Verify = new \Think\Verify();

        return $Verify->check($code,$id);
       
    } 

    public function verify(){
        ob_clean();
        $Verify = new \Think\Verify();
        $Verify->entry();
    }

    public function register(){
    $user=D('users');

    if ($user->create()) {
        
        if ($_FILES['photo']['error']==0) {
            $logoinfo = $this->myupload();
            $pname = $logoinfo['photo']['savename'];
   
        }

        else{
            $pname="default.jpg";
        } 
       

        $image = new \Think\Image();
        $image->open('./Public/Uploads/userphoto/'.$pname);
        $image->thumb(150,150)->save('./Public/Uploads/userphoto/'.$pname);
        
        $user->photo=$pname;

       
        if (false!==$user->add()) {

           
  
            $this->success('注册成功','../Users/login');
        }
        else
        {
           $this->error('数据错误','../Users/reg'); 
        }        
    }

else{
        $this->error($user->getError());
    }
}



    public function myupload(){
        $upload=new \Think\Upload();//实例化ThinkPhp自带上传类
        $upload->maxSize = 3145728;//设置上传附件大小
        $upload->exts = array('jpg','jpeg','png','gif');//设置附件上传类型
        $upload->rootPath = './Public/Uploads/userphoto/';
        //$upload->savePath='';
        $upload->autoSub =false;
        
        $upload->saveName = time().'_'.mt_rand();
        $info = $upload->upload();  // 上传文件
        
        if (!$info) {
            $this->error($upload->getError());
        }
        else{
            return $info;
        }



            }



  public function userinfo(){

        $user = M('users');
        $data['telephone']=$_SESSION['telephone'];
        $res = $user->where($data)->find();
        $this->userInfo=$res;
        //var_dump($res);  
        $foodmodel= M('food');
        $res2 = $foodmodel->where($data)->select();
        $this->userInfo2=$res2;
        $commentsmodel= M('comments');
        $res3 = $commentsmodel->where($data)->select();
        $this->userInfo3=$res3;
        $this->display();
  }


public function editUser()
{       
        $user = M('users');
        $data['telephone']=$_SESSION['telephone'];
        /* dump($data);*/
        $res = $user->where($data)->find();
        $this->userInfos=$res;
        //var_dump($res);

       $this->display();
}

public function doUpdate(){

      //var_dump($_POST['id']);
      //exit();
      $id=$_POST['id'];
      $usermodel=M('users');
    
      $data['telephone']=$_POST['telephone'];
      $data['email']=$_POST['email'];
    
       if(false!==$usermodel->where("id=$id")->save($data)){
         $this->success('修改成功！','../Users/userInfo');
          }
          else{
              $this->error('修改失败！');
            }
     }

     public function logout(){
      
    
    session(null);
        // var_dump($session['telephone']);
         
            $this->success('退出成功！', U('Index/index'));
        
    } 
     public function checkTele(){
      $t=$_GET["t"];
      $userModel = M("Users");
      $rs=$userModel->where("telephone='$t'")->select();
      if($rs) {echo "手机号已存在";}
      else{
        echo "手机号可用";
      }
 
     }
public function delfood($id){
     
    $foodmodel=M('food');
    $res=$foodmodel->where("fid=$id")->find();
    $photoPath=realpath("./Public/Upload/foodphoto/".$res['photo']);
    unlink($photoPath);
    if(false!==$foodmodel->where("fid=$id")->delete()) 
    {
      M('comments')->where("fid=$id")->delete(); 
      $this->success('删除成功');
    }
    else{
      $this->error('删除失败！');
    }
}

}